// Copyright(c) 2008 Tri Tech Information Systems Inc. 
// Distributed under the Boost Software License, Version 1.0.
//     (See accompanying file ../LICENSE_1_0.txt or copy at
//           http://www.boost.org/LICENSE_1_0.txt)
//     
#include <pyxx/pointer.h>
#include <map>
#include <iostream>

using namespace boost::python;
class PointerApiImpl : public PointerApi
{
    public:

    virtual boost::python::object to_python(const void * ptr, boost::python::type_info info)
    {
        map_type::iterator iter = convertors.find(info);
        if( iter != convertors.end() )
        {
            return iter->second(ptr);
        }   
        else
        {
            std::string message = "No pointer handling registered for ";
            message += info.name();
            throw std::logic_error(message);
        }
    }

    virtual void register_to_python(boost::python::type_info info, boost::python::object (*convertor)(const void*) )
    {
        if( convertors.find(info) != convertors.end() ) 
        {
            std::string message = "A pointer handler is already registered for ";
            message += info.name();
        }
        convertors[info] = convertor;
    }

    private:
    typedef object (*ConverterType)(const void*);
    typedef std::map< boost::python::type_info, ConverterType> map_type;
    map_type convertors;
};

void InstallPointerSystem()
{
    scope s;
    s.attr("_pointer_api") = object( handle<>(PyCObject_FromVoidPtr(new PointerApiImpl, NULL)) );
   
    pass_ptr_by_value<char>(); 
}
